#pragma once


#include<assert.h>
#include<iostream>

using namespace std;

namespace yyh
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator cbegin() const
		{
			return _start;
		}

		const_iterator cend() const
		{
			return _finish;
		}

		vector()
			:_start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{ }

		vector(int n, const T& value = T())
		{
			for (int i = 0; i < n; i++)
			{
				push_back(value);
			}
		}

		template<class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			iterator it = first;
			while (it != last)
			{
				push_back(*it);
				it++;
			}
		}

		vector(const vector<T>& v)
		{
			reserve(v.size());
			auto it = v.cbegin();
			while (it != v.cend())
			{
				push_back(*it);
				it++;
			}
		}

		vector<T>& operator =(const vector<T>& v)
		{
			if (this != &v)
			{
				delete[] _start;
				_start = _finish = _end_of_storage = nullptr;
				reserve(v.size());
				auto it = v.cbegin();
				while (it != v.cend())
				{
					push_back(*it);
					it++;
				}
			}
			return *this;
		}

		~vector()
		{
			delete[] _start;
			_start = _finish = _end_of_storage = nullptr;
		}

		size_t size() const
		{
			return _finish - _start;
		}

		size_t capacity() const
		{
			return _end_of_storage - _start;
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t OldSize = size();
				T* temp = new T[n];
				for (int i = 0; i < OldSize; i++)
				{
					temp[i] = _start[i];
				}
				delete[] _start;
				_start = temp;
				_finish = _start + OldSize;
				_end_of_storage = _start + n;
			}
		}

		void resize(size_t n, const T& value = T())
		{
			if (n < size())
			{
				_finish = _start + n;
			}
			else
			{
				reserve(n);
				while (_finish != _start + n)
				{
					*_finish = value;
					_finish++;
				}
			}
		}

		T& operator [](size_t i)
		{
			assert(i < size());
			return _start[i];
		}

		const T& operator [](size_t i) const
		{
			assert(i < size());
			return _start[i];
		}

		void push_back(const T& x)
		{
			if (_finish == _end_of_storage)
			{
				reserve(capacity() == 0 ? 4 : 2 * capacity());
			}
			*_finish = x;
			_finish++;
		}

		void pop_back()
		{
			assert(_finish > _start);
			_finish--;
		}

		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);
		}

		void insert(iterator pos, const T& x)
		{
			assert(pos >= _start);
			assert(pos <= _finish);
			if (_finish == _end_of_storage)
			{
				int OldLen = pos - _start;
				reserve(capacity() == 0 ? 4 : 2 * capacity());
				pos = _start + OldLen;
			}
			auto it = _finish - 1;
			while (it >= pos)
			{
				*(it + 1) = *it;
				it--;
			}
			*pos = x;
			_finish++;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start);
			assert(pos < _finish);
			auto it = pos + 1;
			while (it < _finish)
			{
				*(it - 1) = *it;
				it++;
			}
			_finish--;
			return pos;
		}

	private:
		iterator _start;
		iterator _finish;
		iterator _end_of_storage;
	};

	void test()
	{
		vector<int> v1(5, 1);
		for (auto& e : v1)
		{
			cout << e << ' ';
		}
		cout << endl;
		
		vector<int> v2(v1.begin(), v1.end());
		for (auto& e : v2)
		{
			cout << e << ' ';
		}
		cout << endl;

		vector<int>v3(v2);
		for (auto& e : v3)
		{
			cout << e << ' ';
		}
		cout << endl;

		vector<int> v4;
		v4 = v3;
		for (auto& e : v4)
		{
			cout << e << ' ';
		}
		cout << endl;

		v4.resize(3);
		v4.resize(8);
		v4.resize(20);
		for (auto& e : v4)
		{
			cout << e << ' ';
		}
		cout << endl;

		for (int i = 0; i < v4.size(); i++)
		{
			cout << v4[i] << ' ';
		}
		cout << endl;

		v3.pop_back();
		for (auto& e : v3)
		{
			cout << e << ' ';
		}
		cout << endl;

		v3.swap(v4);
		for (auto& e : v3)
		{
			cout << e << ' ';
		}
		cout << endl;

		v4.insert(v4.begin(), 2);
		for (auto& e : v4)
		{
			cout << e << ' ';
		}
		cout << endl;
		v4.insert(v4.begin() + 3, 2);
		for (auto& e : v4)
		{
			cout << e << ' ';
		}
		cout << endl;
		v4.insert(v4.end(), 2);
		for (auto& e : v4)
		{
			cout << e << ' ';
		}
		cout << endl;

		v4.erase(v4.begin());
		for (auto& e : v4)
		{
			cout << e << ' ';
		}
		cout << endl;
		v4.erase(v4.begin() + 3);
		for (auto& e : v4)
		{
			cout << e << ' ';
		}
		cout << endl;
		v4.erase(v4.end() - 1);
		for (auto& e : v4)
		{
			cout << e << ' ';
		}
		cout << endl;
	}
}